package com.wwtech.epp.ui.activity.work.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.TaskList;
import com.wwtech.epp.ui.activity.work.TaskListFragment;
import com.wwtech.epp.ui.activity.work.contract.TaskListContract;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.work
 * @Description: presenter of TaskListFragment
 * @date 2017/10/04 11:54:28
 */
public class TaskListPresenter implements TaskListContract.TaskListContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final TaskListContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private TaskListFragment mFragment;

    @Inject
    public TaskListPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull TaskListContract.View view, TaskListFragment fragment) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mFragment = fragment;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getTaskListFromServer(String type, int pageNum, int pageSize) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", pageSize + "");
        map.put("pageNum", pageNum + "");
        switch (type) {
            case "我发布的":
                Disposable disposable = httpAPIWrapper.jobCreat(map)
                        .subscribe(new Consumer<TaskList>() {
                            @Override
                            public void accept(TaskList taskList) throws Exception {
                                //isSuccesse
                                KLog.i("onSuccesse");
                                mView.setListData(taskList);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                //onError
                                KLog.i("onError");
                                mView.closeProgressDialog();
                                throwable.printStackTrace();
                            }
                        }, new Action() {
                            @Override
                            public void run() throws Exception {
                                //onComplete
                                KLog.i("onComplete");
                            }
                        });
                mCompositeDisposable.add(disposable);
                break;
            case "分配我的":
                Disposable disposable1 = httpAPIWrapper.jobOwner(map)
                        .subscribe(new Consumer<TaskList>() {
                            @Override
                            public void accept(TaskList taskList) throws Exception {
                                //isSuccesse
                                KLog.i("onSuccesse");
                                mView.setListData(taskList);
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                //onError
                                KLog.i("onError");
                                mView.closeProgressDialog();
                                throwable.printStackTrace();
                            }
                        }, new Action() {
                            @Override
                            public void run() throws Exception {
                                //onComplete
                                KLog.i("onComplete");
                            }
                        });
                mCompositeDisposable.add(disposable1);
                break;
        }
    }
}